﻿import arcade
import random
import math

# 游戏常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Arcade 弹跳球示例"

class BouncingBall(arcade.Sprite):
    def __init__(self, radius=20, color=arcade.color.BLUE):
        super().__init__()
        self.radius = radius
        self.color = color
        self.change_x = random.uniform(-5, 5)
        self.change_y = random.uniform(-5, 5)
        self.width = radius * 2
        self.height = radius * 2
        self.texture = arcade.make_circle_texture(radius * 2, color)
    
    def update(self):
        # 移动球
        self.center_x += self.change_x
        self.center_y += self.change_y
        
        # 边界碰撞检测
        if self.left < 0:
            self.left = 0
            self.change_x *= -1
        elif self.right > SCREEN_WIDTH:
            self.right = SCREEN_WIDTH
            self.change_x *= -1
            
        if self.bottom < 0:
            self.bottom = 0
            self.change_y *= -1
        elif self.top > SCREEN_HEIGHT:
            self.top = SCREEN_HEIGHT
            self.change_y *= -1

class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
        
        # 精灵列表
        self.ball_list = None
        self.player = None
        
        # 游戏状态
        self.score = 0
        self.game_over = False
        
        # 设置背景颜色
        arcade.set_background_color(arcade.color.AMAZON)
    
    def setup(self):
        """设置游戏，初始化变量"""
        # 创建精灵列表
        self.ball_list = arcade.SpriteList()
        self.player = arcade.SpriteCircle(30, arcade.color.RED)
        self.player.center_x = SCREEN_WIDTH // 2
        self.player.center_y = SCREEN_HEIGHT // 2
        
        # 创建多个弹跳球
        for i in range(10):
            ball = BouncingBall(
                radius=random.randint(15, 30),
                color=random.choice([
                    arcade.color.BLUE, 
                    arcade.color.GREEN, 
                    arcade.color.YELLOW,
                    arcade.color.ORANGE,
                    arcade.color.PURPLE
                ])
            )
            ball.center_x = random.randint(ball.radius, SCREEN_WIDTH - ball.radius)
            ball.center_y = random.randint(ball.radius, SCREEN_HEIGHT - ball.radius)
            self.ball_list.append(ball)
    
    def on_draw(self):
        """渲染屏幕"""
        # 清除屏幕
        arcade.start_render()
        
        # 绘制所有精灵
        self.ball_list.draw()
        self.player.draw()
        
        # 绘制分数
        arcade.draw_text(
            f"分数: {self.score}",
            10, SCREEN_HEIGHT - 30,
            arcade.color.WHITE, 20
        )
        
        # 如果游戏结束，显示游戏结束文本
        if self.game_over:
            arcade.draw_text(
                "游戏结束!",
                SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2,
                arcade.color.RED, 50,
                align="center", anchor_x="center", anchor_y="center"
            )
            arcade.draw_text(
                "按R键重新开始",
                SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 60,
                arcade.color.WHITE, 24,
                align="center", anchor_x="center", anchor_y="center"
            )
    
    def on_update(self, delta_time):
        """游戏逻辑更新"""
        if self.game_over:
            return
        
        # 更新球的位置
        self.ball_list.update()
        
        # 检测玩家与球的碰撞
        hit_list = arcade.check_for_collision_with_list(self.player, self.ball_list)
        for ball in hit_list:
            # 移除被击中的球并增加分数
            ball.remove_from_sprite_lists()
            self.score += 1
            
            # 如果所有球都被击中，游戏结束
            if len(self.ball_list) == 0:
                self.game_over = True
    
    def on_mouse_motion(self, x, y, dx, dy):
        """鼠标移动时调用"""
        if not self.game_over:
            self.player.center_x = x
            self.player.center_y = y
    
    def on_key_press(self, key, modifiers):
        """按键处理"""
        if key == arcade.key.R and self.game_over:
            # 重新开始游戏
            self.setup()
            self.score = 0
            self.game_over = False

def main():
    """主函数"""
    game = MyGame()
    game.setup()
    arcade.run()

if __name__ == "__main__":
    main()